package com.milk.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.milk.system.common.util.DateUtils;
import com.milk.system.model.entity.Comments;
import com.milk.system.mapper.CommentsMapper;
import com.milk.system.service.CommentsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jodd.util.StringUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import com.milk.system.model.query.CommentsPageQuery;
import com.milk.system.model.bo.CommentsBO;
import com.milk.system.model.vo.CommentsPageVO;
import com.milk.system.converter.CommentsConverter;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;

/**
 * 评论管理表服务实现类
 *
 * @author milk
 * @since 2024-05-04
 */
@Service
@RequiredArgsConstructor
public class CommentsServiceImpl extends ServiceImpl<CommentsMapper, Comments> implements CommentsService {

    private final CommentsConverter commentsConverter;

    /**
    * 获取评论管理表分页列表
    *
    * @param queryParams 查询参数
    * @return {@link IPage<CommentsPageVO>} 评论管理表分页列表
    */
    @Override
    public IPage<CommentsPageVO> listPagedCommentss(CommentsPageQuery queryParams) {
    
        // 参数构建
        int pageNum = queryParams.getPageNum();
        int pageSize = queryParams.getPageSize();


        // 格式化为数据库日期格式，避免日期比较使用格式化函数导致索引失效
        DateUtils.toDatabaseFormat(queryParams, "startTime", "endTime");
        LambdaQueryWrapper<Comments> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtil.isNotBlank(queryParams.getCustomerName()), Comments::getCustomerName, queryParams.getCustomerName());
        IPage<Comments> boPage = baseMapper.selectPage(new Page<>(pageNum, pageSize), queryWrapper);
        List<Comments> records = boPage.getRecords();
        List<CommentsPageVO> recordsList = new ArrayList<>();
        records.forEach(bo -> {
            CommentsPageVO build = CommentsPageVO.builder()
                    .id(bo.getId())
                    .customerName(bo.getCustomerName())
                    .commentContent(bo.getCommentContent())
                    .rating(bo.getRating())
                    .createdTime(bo.getCreatedTime())
                    .build();
            recordsList.add(build);
        });
        IPage<CommentsPageVO> resultVO = new Page<>();
        resultVO.setCurrent(boPage.getCurrent());
        resultVO.setSize(boPage.getSize());
        resultVO.setTotal(boPage.getTotal());
        resultVO.setRecords(recordsList);
        resultVO.setPages(boPage.getPages());

        // 实体转换
        return resultVO;
    }
    

    
    /**
     * 删除评论管理表
     *
     * @param ids 评论管理表ID，多个以英文逗号(,)分割
     * @return true|false
     */
    @Override
    public boolean deleteCommentss(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除的评论管理表数据为空");
        // 逻辑删除
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .toList();
        return this.removeByIds(idList);
    }
    

}
